﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;

namespace FrameWork.Setting
{
    public partial class SettingPropertyEditor : SettingEditorBase
    {
        public SettingPropertyCollection SettingList { get;private set; }
        public SettingPropertyEditor()
        {
            InitializeComponent();
        }
        public void ResetEditor(object obj,Type ObjType)
        {
            SettingList = new SettingPropertyCollection();
            SettingParentDefineAttribute SettingParAttr = ObjType.GetCustomAttribute<SettingParentDefineAttribute>();
            if (SettingParAttr == null)
            {
                throw (new InvalidOperationException("父容器未标记SettingParentDefineAttribute特性，操作无效！"));
            }
            string SettingParentName;
            if (SettingParAttr.IsNameSetted)
            {
                SettingParentName = SettingParAttr.ParentSettingName;
            }
            else
            {
                SettingParentName = ObjType.Name;
            }
            PropertyInfo[] ObjPropertyList = ObjType.GetProperties();
            foreach (PropertyInfo ProInfo in ObjPropertyList)
            {
                SettingItemDefineAttribute SettingAttr = ProInfo.GetCustomAttribute<SettingItemDefineAttribute>();
                if (SettingAttr != null)
                {
                    SettingPropertyInfoContainer con = new SettingPropertyInfoContainer(ProInfo, obj,false);
                    SettingList.Add(con);
                }
            }
            this.propertyGrid1.SelectedObject = SettingList;
        }
        public void AcceptSetting()
        {
            if(SettingList!=null)
            {
                foreach(SettingPropertyInfoContainer des in SettingList)
                {
                    des.BaseInfo.SetValue(des.BaseObject, des.Value);
                }
            }
        }
    }
}
